home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 2449 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  8.3 KB

  1. Path: keats.ugrad.cs.ubc.ca!not-for-mail
  2. From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: C or C++ for a 14 year old?
  5. Date: 21 Jan 1996 09:05:04 -0800
  6. Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
  7. Message-ID: <4dtro0INNnjh@keats.ugrad.cs.ubc.ca>
  8. References: <Pine.SUN.3.91.960121014008.21849A-100000@merlin.nando.net>
  9. NNTP-Posting-Host: keats.ugrad.cs.ubc.ca
  10.  
  11. In article <Pine.SUN.3.91.960121014008.21849A-100000@merlin.nando.net>,
  12. pretzel  <pretzel@nando.net> wrote:
  13. >Allright, it's time to end all of this crap.  I *AM* a 14 year old and I 
  14. >*AM* learning how to program in C, and I can tell you that C is the best 
  15. >choice for beginners.  It teaches the programming fundamentals from the 
  16. >very start, unlike BASIC, which let's you do whatever you want, or 
  17. >Pascal, which is harder to find support for.  I had a hard time getting 
  18. >started in programming.  I was interested when I was 9 years old, but I 
  19. >didn't know where to go or even what I was looking for.  Eventually, I 
  20. >realized that my Tandy 1000 had a really neat thing on it called 
  21. >GW-BASIC, and things took off from there.  But it wasn't easy.  I learned 
  22. >how to program in BASIC from poorly written examples I found in 
  23. >magazines, and straight from the GW_BASIC manual.  I developed horrible 
  24. >habits (I used so many GOTOs that my programs looked like spaghetti).  
  25. >PLEASE, don't let this happen to your child!!  Start him/her on 
  26. >structured C programming from the very beginning!  I eventually abandoned 
  27. >basic, and started up with C.  I tried to learn it in the same way, and 
  28. >again developed nasty habits.  The absolute best thing that you can do is 
  29. >buy a good book.  I recommend "The Art and Science of C" by Eric S. 
  30. >Roberts.  It is the best text that I have ever seen on the subject, and I 
  31. >have looked through quite a few.  He takes the confusing elements of C 
  32. >and removes them completely by using his own custom function libraries 
  33. >which simplify things like input and strings.  Later, he teaches you how 
  34. >to do these things without his libraries.  He also teaches the aesthetic 
  35. >side of programming, and enforces good technique.  
  36.  
  37. You might be interested in something that Edsger Dijkstra, a great computer
  38. scientist you might one day learn about, once said about BASIC:
  39.  
  40.     It is practically impossible to teach good programming style to
  41.     students that have had prior exposure to BASIC: as potential
  42.     programmers they are mentally mutilated beyond hope of
  43.     regeneration.
  44.     
  45. Dijkstra is known for many things, among which is the research into methods for
  46. formally proving the correctness of algorithms using logical derivation.
  47.  
  48. Having said that, I'd also like to add that loong ago (about 16 or 17 years?) I
  49. started out programming in BASIC and assembly language soon after that. I don't
  50. think that I was impaired in any way. I took up Pascal and later C with no
  51. problems.
  52.  
  53. BASIC teaches you how to decipher spaghetti code, which is a useful skill for
  54. someone doing software maintenance. BASIC and assembly language have taught me
  55. sheer perseverance, if nothing else.
  56.  
  57. I don't believe that using "goto" is bad. If you notice, the C language has it.
  58. It also has things called function pointers, a mechanism that allows you to
  59. store the address of a function inside a variable so that you can later call
  60. it. You can store any address inside this pointer.
  61.  
  62. The C language alone is far too flexible to teach good program design and
  63. implementation, but it certainly allows these to be practised.
  64.  
  65. The control structures of C are not fully general, meaning that the use of a
  66. goto sometimes makes code simpler and clearer. There is no way to exit out of
  67. a nested inner loop, for instance, since the "break" statement doesn't take an
  68. argument. In the scripting language Perl, you can do "break 3;" to jump three
  69. loops out. In C, you can do this by putting a label where you want it, and
  70. doing a goto.
  71.  
  72. I have seen goto used in a lot of programs as a way of handling errors.
  73. Suppose that you have a routine inside an operating system which needs to
  74. allocate three different objects, but there is no support for doing it
  75. atomically. What you do is try the first one. If the allocation doesn't fail,
  76. you try the second, and if that doesn't fail you allocate the third. If that
  77. doesn't fail, you go on with the rest of the routine. However, if any of the
  78. allocations fail, you may want to back up and free the previous successful
  79. allocations so that you don't cause a kernel memory leak. A good way to do this
  80. is to have a bunch of nested gotos:
  81.  
  82. {
  83.     if ((a = makea()) == FAIL)
  84.         goto backout1;
  85.     if ((b = makeb()) == FAIL)
  86.         goto backout2;
  87.     if ((c = makec()) == FAIL)
  88.         goto backout3;
  89.  
  90.     /* success---do the rest     */
  91.  
  92.     return OK;
  93.  
  94. backout3:
  95.     destroyc(c);
  96. backout2:
  97.     destroyb(b);
  98. backout1:
  99.     destroya(a);
  100.     return FAIL;
  101. }
  102.  
  103. This is far more elegant than having all kinds of "structured" code involving
  104. multiple occurences of the same destroy() calls, and multiple return points
  105. from the function. Of course, it might be better if you could somehow grab all
  106. three objects at once in an "all or nothing" atomic call, but the software you
  107. are interfacing with doesn't always work the way you would like it to.
  108.  
  109. The above has a definite structure that can't be called spaghetti code, despite
  110. that it uses goto. Good programming transcends the particular capabilities of
  111. the language you are using. These capabilities don't always lend themselves to
  112. someone's idea of how programming ought to be done.
  113.  
  114. The idea that structured programming can replace "goto" is only a theoretical
  115. result stemming from the 1960's research of people like Dijkstra. This does not
  116. mean that in any particular language if you avoid the use of things like
  117. "goto", your program will become smaller or clearer---only that it can be made
  118. to work identically to the one using goto. This is an abstract result of a sort
  119. of logical proof, which is misinterpreted by some people as a strict
  120. programming guideline to abide by. It may have served well that way in the wake
  121. of the "software crisis" of the 60's (which the authors software engineering
  122. texts just *love* to mention).
  123.  
  124. If you look at the underlying machine code generated by your compiler, you will
  125. find that it's peppered with branches, subroutine calls and jumps. The GNU
  126. compiler will optimize some switch/case constructs using jump tables, which is
  127. roughly analogous to the old ON X GOTO 1000, 1200, 1500 ... in BASIC. In fact,
  128. the computer hardware basically works with nothing but GOTO's. I don't know of
  129. any mainstream CPU architectures which support a lot fancy control structures
  130. directly in hardware, and in cases where things resembling structures do exist,
  131. they are often poorly exploited by compilers.
  132.  
  133. >    Sorry to be so long winded, but I have been following this debate 
  134. >for a while, and I was getting frustrated that no actually 14 year olds 
  135.  
  136. Oh, so 13 and 15 year olds don't count, eh? How do you know how old anyone is?
  137. I don't see too many contributors revealing their ages; even if many 14 year
  138. olds frequent this group, they may not care to identify with you on the basis
  139. of age alone.  Age is such a fleeting thing; as you get older, the difference
  140. matters less, though when you are 14, an 18 year old looks ancient to you!
  141.  
  142. >were commenting.  In closing, I would like to state that if someone is 
  143.  
  144. "In closing?" "long winded?". I _like_ that. You write pretty well. Not a lot
  145. of fourteen year olds can produce a coherent piece of written language. Maybe
  146. you should get over the "14" business and seek out like-minded people who share
  147. your interests regardless of their age; the 'net is great for that! You may go
  148. to a University one day, and will meet people of all ages, from the rare 14
  149. year old taking third year mathematics, to the 50 year old who has decided to
  150. go back to school.
  151.  
  152. >intelligent enough to want to learn how to program, then they are
  153. >certainly intelligent enough to handle C.
  154.  
  155. Well now, just because someone _wants_ to learn how to program doesn't mean
  156. that that person is intelligent. He or she may be attracted by the perception
  157. that programmers are intelligent, but eventually give up when faced with
  158. overcoming the challenges.  There are such "wannabees" in almost every field. 
  159.  
  160. There are many ways to enter the study of computing; learning a system
  161. programming language is not the only way. Some schools teach LISP-like
  162. languages, such as Scheme, to beginners, for instance. Others still rely on
  163. Pascal, Modula-2 and such.
  164. -- 
  165.  
  166.